package nickthissen.iracing.parsing;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;

public class Parsing
{

    public static ArrayList<Element> selectChildren(final Element element, String selector)
    {
        // Return only direct children that match the selector, rather than including sub-children
        Elements all = element.select(selector);
        return Linq.where(all, new Predicate<Element>()
        {
            @Override
            public boolean apply(Element in)
            {
                return in.parent() == element;
            }
        });
    }


	public static String getFilenameWithoutExtension(String path)
	{
		return stripExtension(getFilename(path));
	}	
	
	public static String getFilename(String path)
	{
		if (path == null) return null;
		int pos = path.lastIndexOf("/");
		if (pos == -1) pos = path.lastIndexOf("\\");
		if (pos == -1) return path;
		return path.substring(pos + 1);
	}
	
    public static String stripExtension(String str) 
    {
        if (str == null) return null;
        int pos = str.lastIndexOf(".");
        if (pos == -1) return str;
        return str.substring(0, pos);
    }

}
